import time

from tools.base_page import BasePage


class Shengben(BasePage):
    index = ('xpath', '//*[@text="首页"]')
    coolect_type_course = ('xpath', '//*[@text="课程"]')

    # def zsb_school(self):
    #     school = ('id', 'item_under_graduate_home_model_school_title')
    #     first_school_name = ('xpath', '//*[@resource-id="com.znb.zxx:id/school_recycle"]/android.view.ViewGroup[1]')
    #     first_major = ('xpath', '//*[@resource-id="com.znb.zxx:id/major_recycle"]/android.view.ViewGroup[1]')
    #     self.find(school).click()
    #     self.find(first_school_name).click()
    #     self.find(first_major).click()
    #     self.driver.back()
    #     self.go_swipe(0.5, 0.9, 0.5, 0.1)
    #     time.sleep(1)
    #     self.page(self.index)

    def zsb_course(self):
        zsb_course = ('id', 'item_under_graduate_home_model_course_title')
        first_courst_locator = ('id', 'comm_item_course_title')
        self.find(zsb_course).click()
        self.elements(first_courst_locator)[0].click()

    def exist_course_title(self):
        courseNameTitle = ('id', "courseNameTv")
        if self.is_element(courseNameTitle) == True:
            self.page(self.index)
            return True
        else:
            self.page(self.index)
            return False

    def quan_guidance(self):  # 全指导
        cat_more = ('id', 'timeLineMoreTv')
        toolBarShare = ('id', 'toolBarShare')  # 分享按钮
        share_cancel_tv = ('id', 'share_cancel_tv')  # 取消
        self.find(cat_more).click()
        self.find(toolBarShare).click()
        self.find(share_cancel_tv).click()
        self.page(self.index)

    def zsb_open_course_and_teacher_details(self):
        cat_more_open_course = (
            'xpath', '//*[contains(@resource-id,"com.znb.zxx:id/contentTv") and contains(@text,"查看更多公开课")]')
        courese_list_title = ('xpath',
                              '//*[@class="androidx.recyclerview.widget.RecyclerView"]/android.view.ViewGroup[1]/android.widget.TextView[1]')  # 课程列表中的课程标题
        teacher_name = ('id', 'item_teacher_name_tv')
        teacher_course = ('xpath', '//*[@resource-id="com.znb.zxx:id/title_container"]/android.widget.FrameLayout[2]')
        self.find_by_scroll('查看更多公开课').click()
        self.find(courese_list_title).click()
        self.find(teacher_name).click()
        self.find(teacher_course).click()
        self.swipeUp()
        self.page(self.index)

    def recommend_course_collect(self):
        course_titles = ('id', 'com.znb.zxx:id/comm_item_course_des')  # 专升本推荐课中首个课程的描述
        self.go_swipe(0.5, 0.9, 0.5, 0.1)
        collect_text = ('id', 'wantLeanTv')
        collect = ('id', 'wantLeanIcb')
        time.sleep(3)
        eles = self.elements(course_titles)
        eles[0].click()
        time.sleep(2)
        self.go_swipe(0.5, 0.9, 0.5, 0.1)
        time.sleep(2)
        if self.find(collect_text).text == "想学":
            self.find(collect).click()
        else:
            self.find(collect).click()
            self.find(collect).click()
        self.page(self.index)

    def exist_collect(self, coolect_type, locator, text_value):
        '''
        验证收藏的课程是否添加到我的收藏中
        coolect_type: 收藏分类
        locator：判断是否存在的定位器
        :param text_value: locator定位器的文本值
        :return:
        '''

        my_locator = ('xpath', '//*[@text="我的"]')
        collect = ('xpath', '//*[@text="收藏"]')
        self.find(my_locator).click()
        self.find(collect).click()
        self.find(coolect_type).click()
        eles = self.elements(locator)
        try:
            for ele in eles:
                print(ele.text)
                if ele.text == text_value:
                    self.page(self.index)
                    self.find(self.index).click()
                    return True
        except:
            self.page(self.index)
            self.find(self.index).click()
            return False

    def zsb_news_and_collect(self):
        news_titles = ('id', 'com.znb.zxx:id/comm_item_news_title')
        collect_new = ('id', 'tooBarCollect')
        for num in range(2):
            self.go_swipe(0.5, 0.9, 0.5, 0.1)
        self.elements(news_titles)[0].click()
        time.sleep(3)
        self.find(collect_new).click()
        self.page(self.index)

    def zsb_scool(self):
        school = ('id', 'item_under_graduate_home_model_school_title')
        school_name = ('id', 'school_name')
        school_major = ('id', 'major_tv')
        self.find(school).click()
        time.sleep(1)
        self.elements(school_name)[0].click()
        time.sleep(1)
        self.elements(school_major)[0].click()
        time.sleep(2)

    def exist_zsb_school_major(self):
        major_title = ('id', 'title_light_view')
        if self.is_element(major_title) == True:
            self.page(self.index)
            return True
        else:
            self.page(self.index)
            return False

    def zsb_quan_guidance(self):  # 专升本全指导
        cat_more = ('id', 'com.znb.zxx:id/timeLineMoreTv')
        self.find(cat_more).click()
        time.sleep(2)
        self.page(self.index)
